package com.china08.yxyapi.api.manage.tec.teaching.live.handleLive;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.api.manage.tec.teaching.live.myClassSchedule.MyClassScheduleController;
import com.china08.yxyapi.model.YxyApiReqModel;

import io.swagger.annotations.ApiOperation;

/**
 * 我的教学-直播课-教师-直播去重查询
 * @author 赵亚辉
 *
 */
@CrossOrigin
@RestController
public class LiveDistinctFindController extends YxyApiBaseController{
	
	private static final Logger logger=Logger.getLogger(MyClassScheduleController.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@ApiOperation(value = "直播去重查询")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/live/liveDistinct/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		
		RespModel respModel=new RespModel();
		
		String teacherId = getProfile(request, response).getId();
		
		String dataSQL="select count(1) from yxy_live.yxy_live_lesson lesson,yxy_live.yxy_live_lesson_item lessonItem "
				+" where lesson.id=lessonItem.lesson_id and lesson.teacher_id='"+teacherId+"' ";
		List<ReqLessonItem> list = reqModel.list;
		String timeConditionSQL="";
		if(list.size()>0){
			timeConditionSQL=" and ( ";
			for (int x=0;x<list.size();x++) {
				ReqLessonItem reqLessonItem=list.get(x);
				String startDate=reqLessonItem.startDate;
				String endDate=reqLessonItem.endDate;
				if(StringUtils.isNotEmpty(startDate)&&StringUtils.isNotEmpty(endDate)) {
					if(x>0){
						timeConditionSQL+=" or ";
					}
					timeConditionSQL+=" (DATE_FORMAT(lessonItem.begin_date,'%y-%m-%d %h:%m:%s')<=DATE_FORMAT('"+startDate+"','%y-%m-%d %h:%m:%s')"
							+ " and DATE_FORMAT(lessonItem.end_date,'%y-%m-%d %h:%m:%s')>=DATE_FORMAT('"+endDate+"','%y-%m-%d %h:%m:%s'))";
				}
			}
			timeConditionSQL+=")";
		}
		
		dataSQL+=timeConditionSQL;
		Integer totalCount = jdbcTemplate.queryForObject(dataSQL, Integer.class);
		
		Integer status=totalCount>0?1:0;
		respModel.status=status;
		
		return respModel;
	}
	
	protected static class ReqModel extends YxyApiReqModel{
		
		public List<ReqLessonItem> list;
		
		@Override
        public void autoCheck() {
            Assert.notNull(list.size()==0, "请填写课时时间");
        }
	}
	
	protected static class RespModel {
		
		//0:不存在；1：存在
		public Integer status;
		
	}
	
	protected static class ReqLessonItem{
		
		public String startDate;
		
		public String endDate;
	}

}
